Imports System.Xml.Serialization
Imports System.Text.RegularExpressions

''' <summary>
''' Represents the documentation of an element.
''' </summary>
Public Class Documentation
    Inherits XEntityModelElementBase

    ''' <summary>Initializes an instance of the <see cref="Documentation" /> class.</summary>
    ''' <param name="namespace">The <see cref="XNamespace"/> of the new element.</param>
    Public Sub New(ByVal [namespace] As XNamespace)
        MyBase.New("Documentation", [namespace].ToString())
    End Sub

    ''' <summary>Initializes an instance of the <see cref="Documentation" /> class.</summary>
    ''' <param name="other">An <see cref="System.Xml.Linq.XElement"/> object to copy from.</param>
    Public Sub New(ByVal other As XElement)
        MyBase.New(other)
    End Sub

    ''' <summary>
    ''' Gets or sets the XML code of the summary element.
    ''' </summary>
    Public Property Summary() As String
        Get
            Return GetElementInnerXml(Me.Element(MyBase.Name.Namespace + "Summary"))
        End Get
        Set(ByVal value As String)
            Dim el = Me.Element(MyBase.Name.Namespace + "Summary")
            If (el IsNot Nothing) Then
                el.Remove()
            End If
            Me.Add(New XElement(MyBase.Name.Namespace + "Summary", value))
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the XML code of the long description element.
    ''' </summary>
    Public Property LongDescription() As String
        Get
            Return GetElementInnerXml(Me.Element(MyBase.Name.Namespace + "LongDescription"))
        End Get
        Set(ByVal value As String)
            Dim el = Me.Element(MyBase.Name.Namespace + "LongDescription")
            If (el IsNot Nothing) Then
                el.Remove()
            End If
            Me.Add(New XElement(MyBase.Name.Namespace + "LongDescription", value))
        End Set
    End Property

    Private Function GetElementInnerXml(ByVal element As XElement) As String
        If (element Is Nothing) Then
            Return String.Empty
        End If
        Dim name As String = element.Name.LocalName
        Dim rx As New Regex("\<(?:.*\:)?" & name & ".*?\>(.*?)\</(?:.*\:)?" & name & "\>", RegexOptions.Singleline Or RegexOptions.IgnoreCase)
        Return rx.Replace(element.ToString(), "$1")
    End Function

End Class

